//noinspection JSAnnotator
/**
 * Created by Feng on 2017/2/14.
 */
var vm = new Vue({
    el:"#app",
    data:{
        productList:[],
        totalMoney:0,
        checkAllFlag : false,
        totalPrice:0,
        delFlag:false,   //弹出框的显示影藏
        curProduct:0
    },
    filters:{
        //局部过滤器
        formatMoney:function (value) {
            return "￥" +value.toFixed(2);//给钱数加￥和两位小数
        }
    },
    mounted:function () {
        this.$nextTick(function () {
            this.cartView();
        })
    },
    methods:{
        cartView:function () {
            var _this = this
            this.$http.get("data/cart.json").then(function (res) {
                // _this.productList = res.data.result.list;
                _this.productList = res.body.result.list;
                _this.totalMoney = res.result;
            })
        },
        //商品的加减
        changMoney:function (chang,num) {
            if (num>0){
                chang.productQuentity++;
            }else {
                chang.productQuentity--;
                if (chang.productQuentity<0){
                    chang.productQuentity=0;
                }
            }
            this.calctotalPrice();
        },
        //单选
        selectedP:function (item) {
            if (typeof item.checked == "undefined"){
                // Vue.set(item,"checked",true);//全局
                this.$set(item,"checked",true);//局部
            }else {
                item.checked = !item.checked;
            }
            this.calctotalPrice();//算总价
        },
        //全选
        checkAllP:function (flag) {
            this.checkAllFlag = flag;
            var _this = this;
            this.productList.forEach(function (item,index) {
                if (typeof item.checked == "undefined"){
                    _this.$set(item,"checked",_this.checkAllFlag);
                }else {
                    item.checked = _this.checkAllFlag;
                }
            })
            this.calctotalPrice();
        },
        //单选增加总价
        calctotalPrice:function () {
            var _this = this;
            this.totalPrice = 0;
            this.productList.forEach(function (item,index) {
                if (item.checked){
                    _this.totalPrice += item.productPrice*item.productQuentity;
                }
            })
        },
        //弹出界面
        onClick:function (cur) {
            this.delFlag = true;
            this.curProduct = cur;
        },
        //删除
        delProduct:function () {
            var index = this.productList.indexOf(this.curProduct);
            this.productList.splice(index,1);//从当前开始删除1个
            this.delFlag = false;
        }
    }
});
//全局过滤器(那都可以用)
Vue.filter("money",function (value,type) {
    return "￥" +value.toFixed(2) +type;
})